package com.woniu.tms.studentinfo.service.impl;

import com.woniu.tms.entity.User;
import com.woniu.tms.entity.UserExample;
import com.woniu.tms.entity.UserExample.Criteria;
import com.woniu.tms.entity.UserRole;
import com.woniu.tms.mapper.UserMapper;
import com.woniu.tms.mapper.UserRoleMapper;
import com.woniu.tms.studentinfo.MD5Utils;
import com.woniu.tms.studentinfo.service.studentInfoITestService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class studentInfoTestServiceImpl implements studentInfoITestService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserRoleMapper userRoleMapper;

    @Override
    public boolean batchImport(String fileName, MultipartFile file) throws Exception {
        boolean notNull = false;
        List<User> userList = new ArrayList<User>();
        List<String> pwdList = new ArrayList<String>();
        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            /* throw new MyException("上传文件格式不正确"); */
        }
        boolean isExcel2003 = true;
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }
        InputStream is = file.getInputStream();
        Workbook wb = null;
        if (isExcel2003) {
            wb = new HSSFWorkbook(is);
        } else {
            wb = new XSSFWorkbook(is);
        }
        Sheet sheet = wb.getSheetAt(0);
        if (sheet != null) {
            notNull = true;
        }
        User user;
        List<String> snoList = new ArrayList<>();
        for (int r = 1; r <= sheet.getLastRowNum(); r++) {
            Row row = sheet.getRow(r);
            if (row == null) {
                continue;
            }

            user = new User();

            if (row.getCell(0).getCellType() != 1) {
                throw new RuntimeException("导入失败(第" + (r + 1) + "行,编号请设为文本格式)");
            }

            String sno = row.getCell(0).getStringCellValue();

            if (sno == null || sno.isEmpty()) {
                throw new RuntimeException("导入失败(第" + (r + 1) + "行,编号未填写)");
            }


            snoList.add(sno);

            row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
            String uname = row.getCell(1).getStringCellValue();
            if (uname == null || uname.isEmpty()) {
                throw new RuntimeException("导入失败(第" + (r + 1) + "行,姓名未填写)");
            }


            String upwd = row.getCell(2).getStringCellValue();
            if (upwd == null || upwd.isEmpty()) {
                throw new RuntimeException("导入失败(第" + (r + 1) + "行,密码未填写)");
            }
            upwd = MD5Utils.generate(upwd);


            String ueamail = row.getCell(3).getStringCellValue();
            if (ueamail == null || ueamail.isEmpty()) {
                throw new RuntimeException("导入失败(第" + (r + 1) + "行,邮箱未填写)");
            }


            String uphone = row.getCell(4).getStringCellValue();
            if (uphone == null ||
                    uphone.isEmpty()) {
                throw new RuntimeException("导入失败(第" + (r + 1) + "行,电话未填写)");
            }


            String classid = row.getCell(5).getStringCellValue();
            if (classid == null || classid.isEmpty()) {
                throw new RuntimeException("导入失败(第" + (r + 1) + "行,班级未填写)");
            }

            String ustatus = row.getCell(7).getStringCellValue();
            if (ustatus == null || ustatus.isEmpty()) {
                throw new RuntimeException("导入失败(第" + (r + 1) + "行,状态未填写)");
            }

            String education = row.getCell(8).getStringCellValue();
            if (education == null || education.isEmpty()) {
                throw new RuntimeException("导入失败(第" + (r + 1) + "行,学历未填写)");
            }

            String gender = row.getCell(9).getStringCellValue();
            if (gender == null || gender.isEmpty()) {
                throw new RuntimeException("导入失败(第" + (r + 1) + "行,性别未填写)");
            }


            String city = row.getCell(11).getStringCellValue();
            if (city == null || city.isEmpty()) {
                throw new RuntimeException("导入失败(第" + (r + 1) + "行,城市未填写)");
            }

            Date date;
            if (row.getCell(12).getCellType() != 0) {
                throw new RuntimeException("导入失败(第" + (r + 1) + "行,出生日期格式不正确或未填写)");
            } else {
                date = row.getCell(12).getDateCellValue();
            }

            Integer classids = Integer.parseInt(classid);

            user.setSno(sno);
            user.setUname(uname);
            user.setUpwd(upwd);
            user.setUeamail(ueamail);
            user.setUphone(uphone);
            user.setClassid(classids);
            user.setUstatus(ustatus);
            user.setEducation(education);
            user.setGender(gender);
            user.setCity(city);
            user.setBirthdate(date);

            userList.add(user);

        }

        UserExample example = new UserExample();
        Criteria criteria = example.createCriteria();

        criteria.andSnoIn(snoList);
        List<User> usersList = userMapper.selectByExample(example);


        List<Integer> uidList = new ArrayList<>();
        for (User list : usersList) {
            uidList.add(list.getUid());
        }

        for (Integer uid : uidList) {
            UserRole userRole = new UserRole();
            userRole.setUid(uid);
            userRole.setRid(1);
            userRoleMapper.insert(userRole);
        }


        for (User userResord : userList) {
            String uname1 = userResord.getUname();

            int cnt = userMapper.selectByName(uname1);

            if (cnt == 0) {

                userMapper.addUser(userResord);

                System.out.println(" 插入 " + userResord);
            } else {
                userMapper.updateUserByName(userResord);

                System.out.println(" 更新 " + userResord);
            }
        }

        return notNull;

    }

}
